package com.lidong.zc.databasemodel.init.core;

import com.lidong.zc.databasemodel.constants.enumconst.StrCaseEnum;
import com.lidong.zc.databasemodel.constants.database.CharacterSetEnum;
import com.lidong.zc.databasemodel.entity.database.DataBaseColumn;
import com.lidong.zc.databasemodel.entity.database.DataBaseTable;
import org.springframework.util.ObjectUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 12/1/2022 9:10 PM
 */
public class DataBaseMetaDataHandle extends AbstractCreateTableManage{

    protected DataBaseMetaDataHandle(InitDataBaseContext initDataBaseContext) {
        super(initDataBaseContext);
    }

    @Override
    protected List<DataBaseTable> getDataBaseTableList() throws SQLException {
        return null;
    }

    @Override
    protected void getColumnList(List<? extends DataBaseTable> dataBaseTableList) throws SQLException {

    }

    @Override
    protected void getIndexList(List<? extends DataBaseTable> dataBaseTableList) throws SQLException {

    }

    /**
     * 获取数据库名称
     *
     * @return String
     * @throws SQLException SQLException
     */
    public String getSchema() throws SQLException {
        Connection connection = this.getConnection();
        String schema = connection.getSchema();
        String catalog = connection.getCatalog();
        if (ObjectUtils.isEmpty(schema)) {
            return catalog;
        } else {
            return schema;
        }
    }

    /**
     * 转换字段名称
     *
     * @param column
     * @return
     */
    protected String getColumnName(DataBaseColumn column) {
        String name = column.getName();
        StrCaseEnum nameCase = column.getNameCase();
        if (StrCaseEnum.UPPER == nameCase) {
            name = name.toUpperCase(Locale.ROOT);
        } else {
            name = name.toLowerCase(Locale.ROOT);
        }
        return name;
    }

    /**
     * 获取表 字符编码
     *
     * @param character character
     * @return String
     */
    protected String getTableCharset(CharacterSetEnum character) {
        if (CharacterSetEnum.UTF_8 == character) {
            return "utf8";
        }
        return "utf8";
    }
}
